package com.gewu.pm.ui.popup;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.hjq.base.BaseAdapter;
import com.hjq.base.BasePopupWindow;
import com.hjq.base.action.AnimAction;

import java.util.List;

/**
 * 时间选择
 */
public final class SelectTimePopup {

    public static final class Builder
            extends BasePopupWindow.Builder<Builder>
            implements BaseAdapter.OnItemClickListener {

        @SuppressWarnings("rawtypes")
        private OnListener mListener;
        private boolean mAutoDismiss = true;

        private final SortAdapter mAdapter;
        private final RecyclerView recyclerView;

        public Builder(Context context) {
            super(context);
            setContentView(R.layout.pop_select_time);
            recyclerView = findViewById(R.id.re_view);

            setAnimStyle(AnimAction.ANIM_DEFAULT);

            mAdapter = new SortAdapter(getContext());
            mAdapter.setOnItemClickListener(this);
            recyclerView.setAdapter(mAdapter);

        }

        @SuppressWarnings("all")
        public Builder setList(List<String> data) {
            mAdapter.setData(data);
            return this;
        }

        /**
         * 设置默认选中项（根据索引）
         */
        public Builder setSelectedPosition(int position) {
            mAdapter.setSelectedPosition(position);
            return this;
        }

        /**
         * 设置默认选中项（根据文本内容）
         */
        public Builder setSelectedItem(String item) {
            if (item != null && !item.isEmpty()) {
                List<String> data = mAdapter.getData();
                if (data != null) {
                    for (int i = 0; i < data.size(); i++) {
                        if (item.equals(data.get(i))) {
                            mAdapter.setSelectedPosition(i);
                            break;
                        }
                    }
                }
            }
            return this;
        }

        public Builder setAutoDismiss(boolean dismiss) {
            mAutoDismiss = dismiss;
            return this;
        }

        @SuppressWarnings("rawtypes")
        public Builder setListener(OnListener listener) {
            mListener = listener;
            return this;
        }

        @Override
        public void onClick(View view) {

        }

        /**
         * {@link BaseAdapter.OnItemClickListener}
         */
        @SuppressWarnings("all")
        @Override
        public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
            // 更新选中状态
            mAdapter.setSelectedPosition(position);

            if (mAutoDismiss) {
                postDelayed(new Runnable() {//暂时加一个时间，300毫秒之后关闭弹窗，因为点击列表，UI图上文字要显示红色，如果不加，文字颜色还没变就关闭页面了
                    @Override
                    public void run() {
                        dismiss();
                    }
                },300);
            }

            if (mListener != null) {
                mListener.onSelected(getPopupWindow(), mAdapter.getItem(position));
            }
        }
    }

    private static final class SortAdapter extends AppAdapter<String> {

        private int selectedPosition = -1;

        private SortAdapter(Context context) {
            super(context);
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder();
        }

        public void setSelectedPosition(int position) {
            int oldPosition = selectedPosition;
            selectedPosition = position;
            if (oldPosition != -1) {
                notifyItemChanged(oldPosition);
            }
            notifyItemChanged(selectedPosition);
        }

        private final class ViewHolder extends AppAdapter<?>.ViewHolder {
            private final TextView mTextView;
            private final View mCheckIcon;
            private final View mDivider;

            ViewHolder() {
                super(R.layout.select_time_item);
                mTextView = findViewById(R.id.tv_content);
                mCheckIcon = findViewById(R.id.iv_check);
                mDivider = findViewById(R.id.divider);
            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onBindView(int position) {
                String str = getItem(position);
                mTextView.setText(str);

                // 设置选中状态
                boolean isSelected = (position == selectedPosition);
                mCheckIcon.setVisibility(isSelected ? View.VISIBLE : View.GONE);
                mTextView.setTextColor(getResources().getColor(isSelected ? R.color.red_app : R.color.cb3));
                
                // 最后一项不显示分割线
                mDivider.setVisibility(position == getCount() - 1 ? View.GONE : View.VISIBLE);
            }
        }
    }

    public interface OnListener {

        /**
         * 选择条目时回调
         */
        void onSelected(BasePopupWindow popupWindow, String category);
    }
}